// 示例路由文件
const express = require('express');
const sql = require('mssql');
const router = express.Router();

// 获取所有示例数据
router.get('/data', async (req, res) => {
  try {
    const pool = await sql.connect();
    const result = await pool.request().query('SELECT * FROM [YourTableName]');
    res.json({
      success: true,
      data: result.recordset
    });
  } catch (error) {
    console.error('查询数据时出错:', error.message);
    res.status(500).json({
      success: false,
      message: '查询数据失败',
      error: error.message
    });
  }
});

// 根据ID获取示例数据
router.get('/data/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const pool = await sql.connect();
    const result = await pool.request()
      .input('id', sql.Int, id)
      .query('SELECT * FROM [YourTableName] WHERE id = @id');
    
    if (result.recordset.length === 0) {
      return res.status(404).json({
        success: false,
        message: '未找到数据'
      });
    }
    
    res.json({
      success: true,
      data: result.recordset[0]
    });
  } catch (error) {
    console.error('查询数据时出错:', error.message);
    res.status(500).json({
      success: false,
      message: '查询数据失败',
      error: error.message
    });
  }
});

module.exports = router;